home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Memory / MemHook.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-13  |  3.3 KB  |  135 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MemHook.h
  3.  
  4.     Contains:    MemoryHook class interface
  5.  
  6.     Owned by:    Michael Burbidge, Jens Alfke
  7.     Owned by:    Jens Alfke
  8.  
  9.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  10.  
  11.     Change History (most recent first):
  12.     
  13.          <1>     9/13/96    jpa        first checked in
  14.  
  15.     To Do:
  16.     In Progress:
  17.         
  18. */
  19.  
  20. #ifndef _MEMHOOK_
  21. #define _MEMHOOK_
  22.  
  23. #ifndef _PLATFMEM_
  24. #include "PlatfMem.h"
  25. #endif
  26.  
  27. #ifndef __STDDEF__
  28. //#include <stddef.h>
  29. #endif
  30.  
  31. #ifndef __TYPES__
  32. //#include <Types.h>
  33. #endif
  34.  
  35.  
  36. //========================================================================================
  37. // Forward class declarations
  38. //========================================================================================
  39.  
  40. class MemoryHookList;
  41.  
  42.  
  43. #if MM_DEBUG
  44. //========================================================================================
  45. // ODMemoryHook (MM_DEBUG only)
  46. //
  47. //        A Memory hook that can be used to track heap operations by subclassing ODMemoryHook
  48. //        and overriding the methods of interest. Your subclass is then registered with a
  49. //        MemoryHeap.
  50. //
  51. //========================================================================================
  52.  
  53. class ODMemoryHook
  54. {
  55. public:
  56.     // These methods should all be abstract, but MemoryHookList instantiates a
  57.     // ODMemoryHook to use as the head of its linked list, so here we just use empty
  58.     // inlines. A ODMemoryHook still cannot be instantiated because the constructor
  59.     // is protected.
  60.  
  61.     virtual ODBlockSize GetHeaderSize();
  62.     
  63.     virtual ODBlockSize AboutToAllocate(ODBlockSize size) const;
  64.     virtual void* DidAllocate(void* blk, ODBlockSize size);
  65.     virtual const void* AboutToBlockSize(const void* blk);
  66.     virtual void* AboutToFree(void* blk);
  67.     virtual void AboutToRealloc(void*& , ODBlockSize&);
  68.     virtual void* DidRealloc(void* oldBlk, void* newBlk, ODBlockSize);
  69.     virtual void AboutToReset();
  70.     virtual void Comment(const char*);
  71.         
  72.     virtual long GetType( ) const;            // Returns type of hook
  73.     
  74.     enum{ kNoType = 0 };
  75.  
  76.     virtual ~ODMemoryHook();
  77.  
  78.     void* operator new(SIZE_T size, MMHeapLocation = kMMTempMemory);
  79.     void operator delete(void* ptr);
  80.  
  81. protected:
  82.  
  83.     ODMemoryHook();
  84.  
  85. private:
  86.     ODMemoryHook* fNextHook, * fPreviousHook;
  87.  
  88.     friend MemoryHookList;
  89.     
  90.     ODMemoryHook(const ODMemoryHook& blk);
  91.     ODMemoryHook& operator=(const ODMemoryHook& blk);
  92.         // This class shouldn't be copied.
  93. };
  94. #endif
  95.  
  96.  
  97. #if MM_DEBUG
  98. //========================================================================================
  99. // MemoryHookList (MM_DEBUG only)
  100. //
  101. //        A list of memory hooks. This is a special linked list that assumes the links for
  102. //        the list are fields of ODMemoryHook. This avoids endless recursion were we to
  103. //        allocate nodes for the MemoryHooks here.
  104. //
  105. //========================================================================================
  106.  
  107. class MemoryHookList
  108. {
  109. public:
  110.     MemoryHookList();
  111.  
  112.     void Add(ODMemoryHook* aMemoryHook);
  113.     void Remove(ODMemoryHook* aMemoryHook);
  114.     ODMemoryHook* First()                        const;
  115.     ODMemoryHook* After( ODMemoryHook* )        const;
  116.     ODMemoryHook* Before( ODMemoryHook* )        const;
  117.     ODMemoryHook* Last()                        const;
  118.  
  119.     ~MemoryHookList();
  120.  
  121. private:
  122.     ODMemoryHook fHead;
  123.     
  124.     MemoryHookList(const MemoryHookList& blk);
  125.     MemoryHookList& operator=(const MemoryHookList& blk);
  126.         // This class shouldn't be copied.
  127.         
  128.     void* operator new( size_t )    {return 0;}    // Will never be allocated from the heap
  129.     void  operator delete( void* )    { }
  130. };
  131. #endif
  132.  
  133.  
  134. #endif /*_MEMHOOK_*/
  135.